﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TheSnakeGame;
using TheSnakeGame.Engine.GameObjects;

namespace TheSnakeGame.Engine
{
    internal class FoodManager : IFoodManager
    {
        public List<Food> Foods
        { 
            get; 
            set;
        }

        public FoodManager()
        {
            this.Foods = new List<Food>();
        }

        /// <summary>
        /// Check if there is food that needs to be destroyed
        /// </summary>
        /// <returns></returns>
        public bool IsNewFoodRequared()
        {
            IList<Food> foodsForDestroy = (from f in this.Foods
                           where f.IsForDestruction == true
                           select f).ToList();

            if (foodsForDestroy.Count > 0)
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// Return the count of the food that must be destroyed in this loop.
        /// </summary>
        /// <returns></returns>
        public int GetRequerdFoodCount()
        {
            IList<Food> foodsForDestroy = (from f in this.Foods
                                           where f.IsForDestruction == true
                                           select f).ToList();
            return foodsForDestroy.Count;
        }

        /// <summary>
        /// Create collection of foods.
        /// </summary>
        /// <param name="places"></param>
        /// <returns></returns>
        public IList<Food> GetFoods(IList<Point> places)
        {
            IList<Food> foods = new List<Food>();

            for (int i = 0; i < places.Count; i++)
            {
                Food food = new Food(places[i]);
                foods.Add(food);
            }

            this.Foods.AddRange(foods);
            return foods;
        }

        public void DeleteMarkedForDestruction()
        {
            this.Foods.RemoveAll(x=>x.IsForDestruction == true);
        }
    }
}
